/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.procurement.web;

import java.util.List;
import java.util.Set;

import com.javaeye.procurement.dto.SupplierOrder;
import com.javaeye.procurement.dto.SupplierOrderDetail;
import com.javaeye.procurement.service.IMaterialsReceiveOrderService;
import com.javaeye.procurement.service.ISupplierOrderService;
import com.javaeye.store.dto.Materials;
import com.javaeye.store.dto.MaterialsSKUDetail;
import com.javaeye.store.service.IMaterialsCategoryService;
import com.javaeye.store.service.IMaterialsSKUDetailService;
import com.javaeye.store.service.IMaterialsService;
import com.javaeye.common.web.PageInfo;
import com.opensymphony.xwork2.ActionSupport;

public class SupplierOrderAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7408989332825417843L;
	
	private SupplierCondition condition;
	
	private PageInfo pageInfo;
	
	private List<SupplierOrder> orderList;
	
	private String orderId;
	
	/**
	 * 采购单信息
	 */
	private SupplierOrder order;
	
	/**
	 * 采购单明细物料ID
	 */
	private List<Integer> materialsIds;
	
	/**
	 * 采购单明细物料编码
	 */
	private List<String> materialsNos;
	
	private List<Float> materialsNums;
	
	private List<Double> materialsPrices;
	
	private List<Double> materialsAmounts;
	
	private List<String> planNos;
	
	private List<String> remarks;
	
	private List<String> materialsSkuNames;
	
	/**
	 * 采购单明细ID
	 */
	private List<Integer> detailIds;
	
	/**
	 * 采购单sevice
	 */
	protected ISupplierOrderService service;
	
	/**
	 * 物料sevice
	 */
	protected IMaterialsService materialsService;
	
	/**
	 * 物料分类sevice
	 */
	protected IMaterialsCategoryService categoryService;
	
	protected IMaterialsSKUDetailService materialsSKUDetailService;
	
	private IMaterialsReceiveOrderService materialsReceiveOrderService;
	
	public SupplierOrderAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new SupplierCondition();
		condition.setStatus(new Integer[]{-1});
		condition.setContractNo("");
		condition.setSupplierName("");
		condition.setMaterialsName("");
		condition.setOrderId("");
		condition.setSkuId(-1);
	}
	
	public void setService(ISupplierOrderService service) {
		this.service = service;
	}
	
	public void setMaterialsService(IMaterialsService materialsService) {
		this.materialsService = materialsService;
	}

	public void setCategoryService(IMaterialsCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setMaterialsSKUDetailService(
			IMaterialsSKUDetailService materialsSKUDetailService) {
		this.materialsSKUDetailService = materialsSKUDetailService;
	}

	/**
	 * 查询采购单列表
	 * @return
	 * @throws Exception
	 */
	public String querySupplierOrderList() throws Exception {
		orderList = service.getSupplierOrderList(condition, pageInfo);
		//找出对应的收货单的个数
		for (SupplierOrder order : orderList) {
			long count = materialsReceiveOrderService.getMaterialsReceiveOrderCount(order.getId());
			order.setMaterialsReceiveOrderCount(count);
		}
		return SUCCESS;
	}

	/**
	 * 查询采购单信息
	 * @return
	 * @throws Exception
	 */
	public String querySupplierOrderInfo() throws Exception {
		order = service.getSupplierOrderInfo(orderId);
		Set<SupplierOrderDetail> orderDetails = order.getOrderDetails();
		for (SupplierOrderDetail detail : orderDetails) {
			MaterialsSKUDetail skuDetail = 
				materialsSKUDetailService.getMaterialsSKUDetail(detail.getMaterialsId());
			detail.setStoreNumber(skuDetail.getNumber());
		}
		long count = materialsReceiveOrderService.getMaterialsReceiveOrderCount(orderId);
		order.setMaterialsReceiveOrderCount(count);
		return SUCCESS;
	}
	
	/**
	 * 删除采购单信息
	 * @return
	 * @throws Exception
	 */
	public String removeSupplierOrder() throws Exception {
		service.removeSupplierOrder(orderId);
		return SUCCESS;
	}
	
	/**
	 * 手动结束采购单
	 * @return
	 * @throws Exception
	 */
	public String finishSupplierOrder() throws Exception {
		service.finishSupplierOrder(orderId);
		querySupplierOrderInfo();
		return SUCCESS;
	}
	
	/**
	 * 保存采购单
	 * @return
	 * @throws Exception
	 */
	public String saveSupplierOrder() throws Exception {
		for (int i = 0; i < materialsIds.size(); i++) {
			String materialsNo = materialsNos.get(i);
			Materials materials = materialsService.getMaterialsByCode(materialsNo);
			
			SupplierOrderDetail detail = new SupplierOrderDetail();
			detail.setMaterialsId(materialsIds.get(i));
			detail.setMaterialsNo(materialsNo);
			detail.setMaterialsCategory(materials.getCategory().getName());
			detail.setMaterialsRootCategory(materials.getRootCategory().getName());
			detail.setMaterialsName(materials.getName());
			detail.setMaterialsType(materials.getType());
			detail.setMaterialsSku(materialsSkuNames.get(i));
			detail.setNumber(materialsNums.get(i));
			detail.setUnit(materials.getUnits());
			detail.setPrice(materialsPrices.get(i));
			detail.setAmount(materialsAmounts.get(i));
			detail.setPlanNo(planNos.get(i));
			detail.setRemark(remarks.get(i));
			
			if (detailIds != null && detailIds.size() > 0) {
				if (i < detailIds.size()) {
					detail.setId(detailIds.get(i));
				}
			}
			
			order.addDetail(detail);
		}
		service.saveSupplierOrder(order);
		return SUCCESS;
	}

	public SupplierCondition getCondition() {
		return condition;
	}

	public void setCondition(SupplierCondition condition) {
		this.condition = condition;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public List<SupplierOrder> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<SupplierOrder> orderList) {
		this.orderList = orderList;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public SupplierOrder getOrder() {
		return order;
	}

	public void setOrder(SupplierOrder order) {
		this.order = order;
	}

	public List<Integer> getMaterialsIds() {
		return materialsIds;
	}

	public void setMaterialsIds(List<Integer> materialsIds) {
		this.materialsIds = materialsIds;
	}

	public List<String> getMaterialsNos() {
		return materialsNos;
	}

	public void setMaterialsNos(List<String> materialsNos) {
		this.materialsNos = materialsNos;
	}

	public List<Float> getMaterialsNums() {
		return materialsNums;
	}

	public void setMaterialsNums(List<Float> materialsNums) {
		this.materialsNums = materialsNums;
	}

	public List<Double> getMaterialsPrices() {
		return materialsPrices;
	}

	public void setMaterialsPrices(List<Double> materialsPrices) {
		this.materialsPrices = materialsPrices;
	}

	public List<Double> getMaterialsAmounts() {
		return materialsAmounts;
	}

	public void setMaterialsAmounts(List<Double> materialsAmounts) {
		this.materialsAmounts = materialsAmounts;
	}

	public List<Integer> getDetailIds() {
		return detailIds;
	}

	public void setDetailIds(List<Integer> detailIds) {
		this.detailIds = detailIds;
	}

	public void setPlanNos(List<String> planNos) {
		this.planNos = planNos;
	}

	public List<String> getPlanNos() {
		return planNos;
	}

	public void setRemarks(List<String> remarks) {
		this.remarks = remarks;
	}

	public List<String> getRemarks() {
		return remarks;
	}

	public void setMaterialsSkuNames(List<String> materialsSkuNames) {
		this.materialsSkuNames = materialsSkuNames;
	}

	public List<String> getMaterialsSkuNames() {
		return materialsSkuNames;
	}

	public void setMaterialsReceiveOrderService(
			IMaterialsReceiveOrderService materialsReceiveOrderService) {
		this.materialsReceiveOrderService = materialsReceiveOrderService;
	}
	
}
